<template>
  <div style="height: 100%; overflow: hidden;">
    <div class="login">
      <img src="../images/loginNewpic.png">
      <p>正登陆中...</p>
    </div>
  </div>
</template>
<script>
  import getData from 'src/service/getData.js'
  import * as types from 'src/vuex/types.js'
  import {Toast} from 'mint-ui';
  import {mapState} from 'vuex'
  import {Storage} from 'src/utils/storage.js'
  import {Common} from 'src/utils/common.js'
  import {getCode} from 'src/utils/getCode.js'

  export default {
    name: 'login',
    computed: {
      ...mapState({
        openId: state => state.user.openId
      })
    },
    async created() {
      window.global = this; // 在控制台可跟this一样输出参数
      // 获取请求验证所需的accessToken  // 公众号的全局唯一接口调用凭据
      let accessToken = (await getData.getAccessToken()).content.accessToken;
      Storage.set("accessToken", accessToken, false);
      if (process.env.NODE_ENV === 'production') {
        let code = Tools.getQueryValue("code")
        this.buildLogin(code)
        window.wx.ready(res => {
          Common.wechatShare()  // 邀请分享
        })
      } else {
        this.devLogin(this.openId)
        window.wx.ready(res => {
          Common.wechatShare() // 邀请分享

        })
      }
    },
    methods: {
      async devLogin(openId) {
        let data = await getData.login({
          openId
        });
        if (data.respCode === '00') {
          let info = data.content
          Storage.set("token", info.token, false)
          this.$store.commit(types.GET_USER_DATA, info);
          Storage.set('isLogin', true, true);
          this.$router.push({
            name: 'home'
          })
        } else {
          Storage.set('isLogin', false, true);
          Toast({
            message: data.errorInfo,
            duration: 1000
          });
        }
      },
      //  正常登录
      async buildLogin(code) {
        const jumpPage = sessionStorage.getItem("jumpPage") || ''; // 缓存中获取跳转页面及内容
        if (Storage.get('isLogin', true)) { // 已经登录去跳转页面或首页
          if (jumpPage) {
            this.$router.push({
              path: jumpPage
            });
            Storage.remove('jumpPage', true); // 跳转后删除
          } else {
            this.$router.push({
              name: 'home'
            })
          }
        } else { // 未有登录标志进行请求登录
          let data = await getData.loginPay({
            code
          })
          if (data.respCode === '00') {
            let info = data.content;
            Storage.set("token", info.token, false)
            this.$store.commit(types.GET_USER_DATA, info);
            Storage.set('isLogin', true, true);
            if (jumpPage) {
              this.$router.push({
                path: jumpPage
              });
              Storage.remove('jumpPage', true)
            } else {
              this.$router.push({
                name: 'home'
              })
            }
          } else {
            if (Storage.get('isLogin', true)) {
              this.$router.push({name: "home"})
            } else {
              Storage.set('isLogin', false, true);
              Toast({
                message: data.errorInfo,
                duration: 1000
              });
              getCode()
            }
          }
          return data;
        }
      }
    }
  }
</script>
<style scoped>
  .login {
    margin-top: 32%;
    text-align: center;
  }

  .login p {
    margin-top: 1rem;
    color: #ff718d;
    font-size: 1rem;
  }

  .login img {
    max-width: 100%;
  }
</style>